package com.xxxx.system.controller.admin;


import com.xxxx.server.dto.ResourceDto;
import com.xxxx.server.dto.ResponseDto;
import com.xxxx.server.service.ResourceService;
import com.xxxx.server.util.ValidatorUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/resource")
public class ResourceController {

    public static final String BUSINESS_NAME = "资源";

    @Resource
    private ResourceService resourceService;

    @GetMapping("/list")
    public ResponseDto list() {
        ResponseDto<List<ResourceDto>> responseDto = new ResponseDto<>();

        List<ResourceDto> resourceDtoList = resourceService.list();
        responseDto.setContent(resourceDtoList);
        return responseDto;
    }

    @PostMapping("/save")
    public ResponseDto save(@RequestBody String jsonStr) {
        //  保存校验
        ValidatorUtil.require(jsonStr, "资源");

        ResponseDto<ResourceDto> responseDto = new ResponseDto<>();

        resourceService.save(jsonStr);
        return responseDto;
    }

    /**
     * 资源树查询(将数据库记录转换成树型结构返回)
     * @return
     */
    @GetMapping("/load-tree")
    public ResponseDto loadTree() {
        ResponseDto<List<ResourceDto>> responseDto = new ResponseDto<>();
        List<ResourceDto> resourceDtoList = resourceService.loadTree();
        responseDto.setContent(resourceDtoList);
        return responseDto;
    }
}
